import React, { createContext, useReducer } from 'react';



//获取二级路由的信息
const authReducer = (state, action) => {
    if (!action.type&&action.categories) {
        return {
            ...state,
            categories: action.categories,
        }
    }
    if (!action.type && action.category_id) {
        return {
            ...state,
            category_id: action.category_id,
        }
    }
    if (!action.type && action.articles) {
        return {
            ...state,
            articles: action.articles,
        }
    }
    switch (action.type) {
        case "后端":    
            return {
                ...state,
                category_id: 11,
            }
            // return state
        case "Android":
            return {
                ...state,
                category_id: 31,
            }

        case "前端":
            return {
                ...state,
                category_id: 21,
            }
        
        case "iOS":
            return {
                ...state,
                category_id: 41,
            }
        case "推荐":
     
            return state
        default:
            
            return state
    }
}

// export const tagReducer = (state, action) => {
//     switch (action.type) {
//         case HOT:
//             return {}
//         case NEWEST:
//             return {}
//         case HISTORY:
//             return {}
//         default:
//             return state
//     }
// }


export const initState = {
    categories: [
        // { category_id: 0, category_name: '推荐' },
        // {
        //     category_id: 1,
        //     category_name: '后端',
        //     children: [
        //         { category_id: 11, category_name: 'Java' },
        //         { category_id: 12, category_name: 'Python' },
        //         { category_id: 13, category_name: 'Go' },
        //     ],
        // },
        
    ],
    category_id:0
}
export const AuthContext = createContext(initState)

export const ArtContext = (props) => {
    
    const [state, dispatch] = useReducer(authReducer, initState)
    return (
        <AuthContext.Provider value={{
            state,
            dispatch
        }}>
            {props.children}
        </AuthContext.Provider>
    )
}